﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class admin_admin_property_image_main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string hash = WebhelperMethods.URLHash(Request);
        if (string.IsNullOrWhiteSpace(hash))
        {
            Response.Redirect("login.aspx");
            return;
        }

        PomplingCustomers customer = PomplingCustomers.CustomerSelect(hash);
        if (string.IsNullOrWhiteSpace(customer.PomplingCustomerID))
        {
            Response.Redirect("login.aspx");
            return;
        }

        string sAddressId = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request);
        if (string.IsNullOrWhiteSpace(sAddressId))
        {
            Response.Redirect("admin_save_address.aspx?c=" + Request["c"] + "&a=");
            return;
        }
        List<PropertyImage> images = PropertyImage.SelectMainImage(Int32.Parse(sAddressId));

        if (images.Count == 0)
        {
            BtnNext.Enabled = false;
            BtnDeleteMainImage.Enabled = false;
            return;
        }

        divMainImage.InnerHtml = "<img src=\"../" + images[0].DisplayPicturePath + "\" height=\"200px\" width=\"200px\" /> ";
        //BtnSaveMainImage.Enabled = false;
    }
   
    protected void BtnSaveImage_Click(object sender, EventArgs e)
    {
        string customerID = WebhelperMethods.GetUrlParamValue("c", Request);
        int addressID = Int32.Parse(WebhelperMethods.GetUrlParamValue("a", Request));

        if (FileUpload1.HasFile)
        {
            try
            {
                string folderNameWeb = PropertyImage.ImageSaveWebPath();
                string dirPath = Server.MapPath("..") + "\\" + PropertyImage.ImageSaveFilePath();
               
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);

                string fileName = WebhelperMethods.SecondsSinceUnixTime().ToString() + FileUpload1.FileName;
                string filePath = dirPath + "\\" + fileName;
                FileUpload1.SaveAs(filePath);
                string webImagePath = folderNameWeb + "/" + fileName;
                PropertyImage pi = new PropertyImage();
                pi.PomplingAddressID = addressID;
                pi.DisplayPicturePath = webImagePath;
                pi.IsMainImage = true;
                pi.WebPropertiesAttributeID = -1;
                pi.PictureTitle = "Main Image";
                pi.PomplingCustomerID = customerID;
                pi.SaveImagePath("INSERT");
                string newUri = "redirect.aspx" + Request.Url.Query + "&pagefrom=admin_property_image_main";
                Response.Redirect(newUri);
            }
            catch
            {
                throw;
            }
        }
        else
        {
            //write error to db
            //LblMessage.Text = "You have not specified a file.";
        }
    }
    protected void BtnNext_Click(object sender, EventArgs e)
    {
        string newUri = "admin_property_images_upload.aspx" + Request.Url.Query;
        Response.Redirect(newUri);
    }
    protected void BtnPrevious_Click(object sender, EventArgs e)
    {
        string newUri = "admin_save_property_attributes.aspx" + Request.Url.Query;
        Response.Redirect(newUri);
    }
    protected void BtnDeleteMainImage_Click(object sender, EventArgs e)
    {
        string customerID = WebhelperMethods.GetUrlParamValue("c", Request);
        int pomplingAddressID = Int32.Parse(WebhelperMethods.GetUrlParamValue("a", Request));
        PropertyImage pi = new PropertyImage(customerID, pomplingAddressID, true, string.Empty);
        pi.WebPropertiesAttributeID = -1;
        pi.PictureTitle = "Main Image";
        pi.DeleteImagePath();

        string newUri = "redirect.aspx" + Request.Url.Query + "&pagefrom=admin_property_image_main";
        Response.Redirect(newUri);
    }
}
